//
//  WBMainTabBarController.swift
//  微博
//
//  Created by lipengfei on 15/9/3.
//  Copyright © 2015年 lipengfei. All rights reserved.
//

import UIKit

class WBMainTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        addChildControllers()
//        添加子控制器
    }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        setButton()
    }
    /// 设置button的位置
    private func setButton(){
//    在tabBar 中添加一个按钮，设置按钮的位置
//        获取子控制器的个数
        let count = childViewControllers.count
//        计算按钮的宽度
        let width = tabBar.bounds.width / CGFloat(count)
//        移动按钮的位置
        let rect = CGRectMake(0, 0, width, tabBar.bounds.height)
        btn.frame = CGRectOffset(rect, 2 * width, 0)
        
    
    }
    ///将子控制器添加到控制器中
    private func addChildControllers(){
        view.tintColor = UIColor.orangeColor()
        addchildViewcontroller(WBHomeController(), title: "主页", imageName: "tabbar_home")
        addchildViewcontroller(WBMessageViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(UIViewController())
        addchildViewcontroller(WBDiscoverViewController(), title: "发现", imageName: "tabbar_discover")
        addchildViewcontroller(WBProfileViewController(), title: "我", imageName: "tabbar_profile")
        
    }
    /// 添加自控制器
    private func addchildViewcontroller(vc:UITableViewController, title:String, imageName:String){
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        let nv = UINavigationController(rootViewController: vc)
//       添加tabBarController中的子控制器
        addChildViewController(nv)
    }
    ///懒加载，向tabBar中添加一个button控件，
    private lazy var btn:UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        self.tabBar.addSubview(button)
        return button
    }()
    
  
}
